package com.gopay.bis.web.controller.ebank;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ccb.ebank.pay.CcbB2bMessage;

/**
 * 
 * CcbB2bFrontRes.java
 * @author LiLiLi
 */
@Controller
@Scope("prototype")
public class CcbB2bFrontRes extends BankResBase {
	private static Log logger = LogFactory.getLog(CcbB2bFrontRes.class);
	@Resource(name = "ccbConfig")
	protected Map<String, String> ccbConfig;
    @RequestMapping(value = "/CcbB2bFrontRes")
    
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
        logger.error("--------------- CcbB2bFrontRes bankback ---------------");
        return super.execute(request, response);
    }

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
	       CcbB2bMessage message = new CcbB2bMessage();
	       // 银行码赋值
	       message.setBank(BankConstants.BANK_LOGO_CCB);
	        // 商户柜台代码
	        message.setPostId(request.getParameter("MPOSID"));
	        // 订单号
	        message.setOrderId(request.getParameter("ORDER_NUMBER"));
	        // 付款客户号
	        message.setCustId(request.getParameter("CUST_ID"));
	        // 付款账号
	        message.setAccNo(request.getParameter("ACC_NO"));
	        // 付款金额
	        message.setOrderAmt(request.getParameter("AMOUNT"));
	        // 支付结果 2、3、4:支付成功、5:交易失败 6:交易不确定
	        message.setStatus(request.getParameter("STATUS"));
	        // 备注1
	        message.setRemark1(request.getParameter("REMARK1"));
	        // 备注2
	        message.setRemark2(request.getParameter("REMARK2"));
	        // 付款方式 6:网上支付转账、7:网上支付普通e票、 8:晚上支付保付e票
	        message.setTranFlag(request.getParameter("TRAN_FLAG"));
	        // 交易时间
	        message.setTranTime(request.getParameter("TRAN_TIME"));
	        // 数字签名
	        message.setSignMessage(request.getParameter("SIGNSTRING"));
	        // B2B公钥
	        message.setPublicKey(ccbConfig.get("CCB.B2B.PBLIC.KEY"));
	        String accName = request.getParameter("ACC_NAME");
	        if (StringUtils.isNotBlank(accName)) {
	        	try {
					accName = new String(accName.getBytes("ISO8859-1"),"GBK");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
	        }
	        String branchName = request.getParameter("BRANCH_NAME");
	        if (StringUtils.isNotBlank(branchName)) {
	        	try {
					branchName = new String(branchName.getBytes("ISO8859-1"),"GBK");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
	        }
	        // 付款账户名称
	        message.setAccName(accName);
	        // 付款分行名称
	        message.setBranchName(branchName);
	        message.setBankOrderId(request.getParameter("ORDER_NUMBER"));
	        message.setAmount(new BigDecimal(request.getParameter("AMOUNT")));
	        message.setFront(true);
			//银行业务产品01企业网银支付
			message.setBankBusProd(BisBankBusinessProd._01);
	        return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request,Result result) {		
        OrderInfo orderInfo = new OrderInfo();
        CcbB2bMessage message = (CcbB2bMessage) result.getModel("res");
		if(message!=null){
			if(message.getStatus().equals("2") || message.getStatus().equals("3") || 
        			message.getStatus().equals("4")){
				orderInfo.setOrderId(message.getOrderId());
			    orderInfo.setAmt(message.getOrderAmt());
			    orderInfo.setNotifyNum("1");
		        orderInfo.setPayStatus("00");
		        orderInfo.setRetCode("00");
		        orderInfo.setRetMsg("");
			}else{
				 orderInfo.setOrderId(message.getOrderId());
		         orderInfo.setAmt(message.getOrderAmt());
		         orderInfo.setNotifyNum("1");
		         orderInfo.setPayStatus("01");
		         orderInfo.setRetCode("01");
		         orderInfo.setRetMsg("");
			}
			orderInfo.setBankName("ccb");
			orderInfo.setIsNotify(false);
		}
        return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		 if (StringUtils.isEmpty(request.getParameter("MPOSID"))
					|| StringUtils.isEmpty(request.getParameter("CUST_ID"))
					|| StringUtils.isEmpty(request.getParameter("ORDER_NUMBER"))
					|| StringUtils.isEmpty(request.getParameter("AMOUNT"))
					|| StringUtils.isEmpty(request.getParameter("STATUS"))
					|| StringUtils.isEmpty(request.getParameter("SIGNSTRING"))) {
				return false;
			}
		return true;
	}
}
